import mitt from 'mitt';
import { onMounted, onUnmounted } from 'vue';
import { EventsCommon } from './common';
import { EventsTable } from './table';

export type Events = EventsCommon & EventsTable;

export const emitter = mitt<Events>();

export const useEmitter = <T extends keyof Events>(event: T, handler: (payload: Events[T]) => void) => {
  onMounted(() => {
    emitter.on(event, handler);
  });
  onUnmounted(() => {
    emitter.off(event, handler);
  });
};
